package com.ruoyi.repayment.service;


import com.ruoyi.repayment.mapper.BenjinMapper;
import com.ruoyi.repayment.mapper.LoanMapper;
import com.ruoyi.repayment.pojo.Benjin;
import com.ruoyi.repayment.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class PrincipalService {

    @Autowired
    LoanMapper loanMapper;

    @Autowired
    AverageCapitalUtils averageCapitalUtils;

    @Autowired
    BenjinMapper benjinMapper;

    /**
     *  等额本金计算方法：
     *      * 每月还款额=每月还款本金+每月还款利息；
     *      * 每月还款本金=贷款总额/贷款月数 ；
     *      * 每月还款利息=贷款本金余额*贷款月利率（ 贷款月利率=年利率/12）；
     *      * 贷款本金余额=贷款总额-已还款月数*每月还款本金。
     */
    public void principalll(Integer id, Integer productId,Integer userId) {
        Benjin benjin = new Benjin();
        Product list=loanMapper.findByList(productId);
        Integer totalMonth=list.getPeriods();
        double invest=list.getMaxquota();
        double yearRate=list.getInterestRate();
        benjin.setApplyforId(id);
        benjin.setUserId(userId);
        for (int i = 1; i <totalMonth+1; i++) {
            benjin.setMonths(i);
            // 每月本金
            double monthPri = averageCapitalUtils.getPerMonthPrincipal(invest, totalMonth);
            // 获取月利率
            double monthRate = yearRate / 12;
            monthRate = new BigDecimal(monthRate).setScale(6, BigDecimal.ROUND_DOWN).doubleValue();
            double monthRes = monthPri + (invest - monthPri * (i - 1)) * monthRate;
            monthRes = new BigDecimal(monthRes).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
            benjin.setMents(monthRes);
            //每月偿还利息


            double principal = averageCapitalUtils.getPerMonthPrincipal(invest, totalMonth);
            Map<Integer, Double> map = averageCapitalUtils.getPerMonthPrincipalInterest(invest, yearRate, totalMonth);

            benjin.setPrincipal(map.get(i).byteValue());
//            for (Map.Entry<Integer, Double> entry : map.entrySet()) {
//                BigDecimal principalBigDecimal = new BigDecimal(principal);
//                BigDecimal principalInterestBigDecimal = new BigDecimal(entry.getValue());
//                BigDecimal interestBigDecimal = principalInterestBigDecimal.subtract(principalBigDecimal);
//                interestBigDecimal = interestBigDecimal.setScale(2, BigDecimal.ROUND_DOWN);
//                inMap.put(entry.getKey(), interestBigDecimal.doubleValue());
//            }
            benjin.setInterests(benjin.getMents()-benjin.getPrincipal());
            //每个偿还本金
            double perMonthPrincipal = averageCapitalUtils.getPerMonthPrincipal(invest, totalMonth);
            benjin.setPrincipal(perMonthPrincipal);
            double interestCount = averageCapitalUtils.getInterestCount(invest, yearRate, totalMonth);
            //总利息
            benjin.setInterestCount(interestCount);
            benjin.setZprice(benjin.getInterestCount()+invest);

            if(benjin.getFigure()==0.00){
                benjin.setFigure(invest-benjin.getPrincipal());
            }else {

                benjin.setFigure(benjin.getFigure()-benjin.getPrincipal());
                if (benjin.getFigure()<0){
                    benjin.setFigure(0.00);
                }
            }
            benjinMapper.insertintos(benjin);
        }
    }

    public List<Benjin> benjinxq(Integer id,Integer userId) {
        List<Benjin> list=benjinMapper.benjinxq(id,userId);
        return list;
    }
}
